package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺操作接口")
@Slf4j
public class ShopController {

    //注入redisTemplate对象
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 修改店铺状态
     * @param status
     * @return
     */
    @ApiOperation(value = "修改店铺状态")
    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status){
        log.info("修改店铺状态为：{}",status == 1 ? "营业" : "停业");

        //使用redis缓存店铺状态
        redisTemplate.opsForValue().set("SHOP_STATUS",status);

        //返回结果
        return Result.success();
    }

    /**
     * 获取店铺状态
     * @return
     */
    @ApiOperation(value = "获取店铺状态")
    @GetMapping("/status")
    public Result<Integer> getStatus(){
        //读取redis缓存内容
        Integer shop_status = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");

        log.info("店铺状态为：{}",shop_status == 1 ? "营业" : "停业");

        //返回结果
        return Result.success(shop_status);
    }
}
